/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KlantenOverzicht.java
 *
 * Created on 14-nov-2011, 21:03:40
 */
package view.manage.customers;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Klant;

/**
 *
 * @author Marco
 */
public class CustomerOversight extends javax.swing.JPanel {
    //Wich column needs to be used for sorting.
    public String sortBy;
    public String order;
    
    private final String ASCEND = "ASC";
    private final String DESCEND = "DESC";
    
    //The default search column.
    private final String DEFAULTSEARCH = "gebruiker_id";
    
    private CustomerData customers;
    /** Creates new form KlantenOverzicht */
    public CustomerOversight() {
        sortBy = "gebruiker_id";
        order = this.ASCEND;
        
        initComponents();
        setKlanten();
        
        //Set the mouselistener for the header in the table.
        jTableCustomers.getTableHeader().addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableHeaderMouseClicked(evt);
            }
        });
    }
    //Put all customers inside the table.
    private void setKlanten() {
        //Remove all rows
        ((DefaultTableModel)jTableCustomers.getModel()).setNumRows(0);
        
        List<Klant> klanten = WinkelApplication.getQueryManager().getKlantList(sortBy, order);
        DefaultTableModel model = (DefaultTableModel) this.jTableCustomers.getModel();
        for (Klant klant : klanten) {
            model.addRow(new Object[]{ new Integer(klant.getGebruiker_id()),
                        klant.getNaam(),
                        klant.getAdres(),
                        klant.getPostcode(),
                        klant.getWoonplaats() });
        }
        customers = new CustomerData(klanten);
    }
    
    private void searchKlanten(List<String> searchBy, String searchQuery) {
        List<Klant> klanten = WinkelApplication.getQueryManager().searchKlantList(searchBy, searchQuery);
        DefaultTableModel model = (DefaultTableModel) this.jTableCustomers.getModel();
        for (Klant klant : klanten) {
            model.addRow(new Object[]{ new Integer(klant.getGebruiker_id()),
                        klant.getNaam(),
                        klant.getAdres(),
                        klant.getPostcode(),
                        klant.getWoonplaats() });
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCustomers = new javax.swing.JTable();
        jButtonAddCustomer = new javax.swing.JButton();
        jTextSearch = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jBoxSearch = new javax.swing.JCheckBox();
        jBoxUser_id = new javax.swing.JCheckBox();
        jBoxName = new javax.swing.JCheckBox();
        jBoxAdres = new javax.swing.JCheckBox();
        jBoxPostalCode = new javax.swing.JCheckBox();
        jBoxHome = new javax.swing.JCheckBox();
        jButtonBackward = new javax.swing.JButton();
        jButtonForward = new javax.swing.JButton();
        jButtonRemoveCustomer = new javax.swing.JButton();
        jButtonSaveChanges = new javax.swing.JButton();

        jTableCustomers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "gebruiker_id", "naam", "adres", "postcode", "woonplaats"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCustomers.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableCustomers);
        jTableCustomers.getColumnModel().getColumn(4).setResizable(false);

        jButtonAddCustomer.setText("Toevoegen");
        jButtonAddCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddCustomerActionPerformed(evt);
            }
        });

        jTextSearch.setEnabled(false);

        jButtonSearch.setText("zoek");
        jButtonSearch.setEnabled(false);
        jButtonSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSearchMouseClicked(evt);
            }
        });

        jLabel1.setText("Zoeken naar:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Klanten");

        jBoxSearch.setText("zoeken:");
        jBoxSearch.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jBoxSearchItemStateChanged(evt);
            }
        });

        jBoxUser_id.setText("gebruiker_id");
        jBoxUser_id.setEnabled(false);

        jBoxName.setText("naam");
        jBoxName.setEnabled(false);

        jBoxAdres.setText("adres");
        jBoxAdres.setEnabled(false);

        jBoxPostalCode.setText("postcode");
        jBoxPostalCode.setEnabled(false);

        jBoxHome.setText("woonplaats");
        jBoxHome.setEnabled(false);

        jButtonBackward.setText("Vorige");
        jButtonBackward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackwardActionPerformed(evt);
            }
        });

        jButtonForward.setText("Volgende");
        jButtonForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonForwardActionPerformed(evt);
            }
        });

        jButtonRemoveCustomer.setText("Verwijder geselecteerde gebruiker");
        jButtonRemoveCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveCustomerActionPerformed(evt);
            }
        });

        jButtonSaveChanges.setText("Wijzigingen opslaan");
        jButtonSaveChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveChangesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonSaveChanges, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemoveCustomer))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jBoxHome)
                                    .addComponent(jBoxPostalCode)
                                    .addComponent(jBoxAdres)
                                    .addComponent(jBoxName)
                                    .addComponent(jBoxUser_id)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jTextSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jBoxSearch))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jButtonBackward, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonForward)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 396, Short.MAX_VALUE)
                                .addComponent(jButtonAddCustomer)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRemoveCustomer)
                    .addComponent(jButtonSaveChanges))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonBackward)
                            .addComponent(jButtonForward))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addComponent(jBoxSearch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBoxUser_id)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBoxName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBoxAdres)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBoxPostalCode)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBoxHome)
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1)
                        .addGap(16, 16, 16))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonAddCustomer))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(333, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddCustomerActionPerformed
        // TODO add your handling code here:
        WinkelApplication.window.newWindow(new AddCustomer());
    }//GEN-LAST:event_jButtonAddCustomerActionPerformed

    private void jButtonSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSearchMouseClicked
        // TODO add your handling code here:
        //Remove all cells in the table.
        ((DefaultTableModel)jTableCustomers.getModel()).setNumRows(0);

        boolean defSearch = true;
        //Put all columns that need to be searched in 'searchBy'
        List<String> searchBy = new java.util.ArrayList<String>();
        if(jBoxAdres.isSelected()) {
            searchBy.add("adres");
            defSearch = false;
        }
        if(jBoxHome.isSelected()) {
            searchBy.add("woonplaats");
            defSearch = false;
        }
        if(jBoxUser_id.isSelected()) {
            searchBy.add("gebruiker_id");
            defSearch = false;
        }
        if(jBoxName.isSelected()) {
            searchBy.add("naam");
            defSearch = false;
        }
        if(jBoxPostalCode.isSelected()) {
            searchBy.add("postcode");
            defSearch = false;
        }
        if(defSearch) {
            searchBy.add(this.DEFAULTSEARCH);
        }
        
        String searchFor = jTextSearch.getText();
        searchKlanten(searchBy, searchFor);
    }//GEN-LAST:event_jButtonSearchMouseClicked
    
    //Enable or disable the components used for searching the database.
    private void jBoxSearchItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jBoxSearchItemStateChanged
        // TODO add your handling code here:
        boolean enable = (evt.getStateChange() == ItemEvent.SELECTED);
        
        jBoxAdres.setEnabled(enable);
        jBoxPostalCode.setEnabled(enable);
        jBoxHome.setEnabled(enable);
        jBoxName.setEnabled(enable);
        jBoxUser_id.setEnabled(enable);
        jTextSearch.setEnabled(enable);
        jButtonSearch.setEnabled(enable);
    }//GEN-LAST:event_jBoxSearchItemStateChanged

    private void jButtonBackwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackwardActionPerformed
        // TODO add your handling code here:
        WinkelApplication.window.previous();
    }//GEN-LAST:event_jButtonBackwardActionPerformed

    private void jButtonForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonForwardActionPerformed
        // TODO add your handling code here:
        WinkelApplication.window.next();
    }//GEN-LAST:event_jButtonForwardActionPerformed

    private void jButtonRemoveCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveCustomerActionPerformed
        // TODO add your handling code here:
        //Are you sure you want to remove the customer?
        if(JOptionPane.showConfirmDialog(WinkelApplication.getInstance().getMainWindow(), "Deze actie zal de geselecteerde gebruikers deleten.\n"
                + "Weet u zeker dat u wilt doorgaan?") != 0) {
            return;
        }
        
        List<String> klanten = new ArrayList();
        
        int row = jTableCustomers.getSelectedRow();
        while(row != -1) {
            String gebruiker_id = ""; 
            gebruiker_id += jTableCustomers.getModel().getValueAt(row, 0);
            klanten.add(gebruiker_id);            
            
            ((DefaultTableModel)jTableCustomers.getModel()).removeRow(row);
            row = jTableCustomers.getSelectedRow();
        }
        WinkelApplication.getQueryManager().delKlant(klanten);
    }//GEN-LAST:event_jButtonRemoveCustomerActionPerformed

    private void jButtonSaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveChangesActionPerformed
        // TODO add your handling code here:
        //Are you sure you want to save changes?
        if(JOptionPane.showConfirmDialog(WinkelApplication.getInstance().getMainWindow(), "Deze actie zal de gewijzigde gegevens pernament gewijzigd maken..\n"
                + "Weet u zeker dat u wilt doorgaan?") != 0) {
            return;
        }
        int shit = jTableCustomers.getRowCount();
        for(int x = 0; x < shit; x++) {
            String name = (String)jTableCustomers.getValueAt(x, 1);
            String adres = (String)jTableCustomers.getValueAt(x, 2);
            String postalCode = (String)jTableCustomers.getValueAt(x, 3);
            String home = (String)jTableCustomers.getValueAt(x, 4);
            customers.compareRow(x, name, adres, postalCode, home);
        }
    }//GEN-LAST:event_jButtonSaveChangesActionPerformed
    private void jTableHeaderMouseClicked(java.awt.event.MouseEvent evt) {
        //Gets the name of the column at the mouse position.
        int column = jTableCustomers.getTableHeader().columnAtPoint(jTableCustomers.getTableHeader().getMousePosition());
        String columnName = jTableCustomers.getColumnName(column);
        
        //Determine the order for sorting.
        if(!sortBy.equals(columnName)) {
            //Make sure the order equels ASCEND
            order = this.ASCEND;
            sortBy = columnName;
        }
        else if(order.equals(this.ASCEND)) {
            order = this.DESCEND;
        }
        else {
            order = this.ASCEND;
        }
        
        setKlanten();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jBoxAdres;
    private javax.swing.JCheckBox jBoxHome;
    private javax.swing.JCheckBox jBoxName;
    private javax.swing.JCheckBox jBoxPostalCode;
    private javax.swing.JCheckBox jBoxSearch;
    private javax.swing.JCheckBox jBoxUser_id;
    private javax.swing.JButton jButtonAddCustomer;
    private javax.swing.JButton jButtonBackward;
    private javax.swing.JButton jButtonForward;
    private javax.swing.JButton jButtonRemoveCustomer;
    private javax.swing.JButton jButtonSaveChanges;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableCustomers;
    private javax.swing.JTextField jTextSearch;
    // End of variables declaration//GEN-END:variables
}
